import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.io.*;

/**
 * This is the deck class. It holds all the cards as a base reference.
 * 
 * @author Sean Wheeler
 *
 */
public class Deck 
{
	private ArrayList<Card> cards; //The storage of all the cards you own
	private ArrayList<Card> drawcards; //The randomized deck you will draw from
	
	
	
	/**
	 * Creates an empty deck with an empty shuffled counterpart.
	 */
	public Deck()
	{
		cards = new ArrayList<Card>();
		drawcards = shuffle();
	}
	
	
	/**
	 * Creates a deck with its shuffled counterpart
	 * @param cards The card list to store
	 */
	public Deck(ArrayList<Card> cards)
	{
		this.cards = cards;
		drawcards = shuffle();
	}
	
	/**
	 * Create a deck from a database, reading it in.
	 * @param fis The file input stream
	 */
	public Deck(FileInputStream fis)
	{
		cards = new ArrayList<Card>();
		
		//Do Stuff Here
		
		drawcards = shuffle();
	}
	
	
	/**
	 * Draw a card from you list of draw cards
	 * @return
	 */
	public Card drawCard()
	{
		if(drawcards.size() == 0)
			drawcards = shuffle();
		return drawcards.remove(0);
	}
	
	
	/**
	 * Shuffle the base deck. If it is empty, the shuffled is also empty
	 * @return a shuffled version of your deck
	 * @throws InterruptedException 
	 */
	public ArrayList<Card> shuffle()
	{
		ArrayList<Card> shuffled = new ArrayList<Card>();
		ArrayList<Card> tempList = new ArrayList<Card>();
		for(int i=0; i< cards.size(); i++) tempList.add(cards.get(i).duplicate());
		Card temp;
		int i = cards.size();
		Random r = new Random(System.currentTimeMillis());
		while (i > 0)
		{
			int index = r.nextInt(i);
			temp = tempList.get(index);
			shuffled.add(new Card(temp.getName(),temp.getActiveDescription(),temp.getPassiveDescription(),temp.getCost(), temp.getPriority(), temp.getType(), temp.getActives(), temp.getPassives()));
			tempList.remove(index);
			i--;
		}
		try{Thread.sleep(11);}catch(Exception e){} //This is done to insure different times are used when shuffling the decks
		if (cards.size() == 0) System.out.print("Problem");
		return shuffled;
		
	}
	
	public void reshuffle(){
		this.drawcards = shuffle();
	}
	
	public String toString()
	{
		String s = "Deck: ";
		for (Card c: cards)
		{
			s += c.toString();
		}
		
		s+= "\nShuffledDeck: ";
		
		for (Card c: drawcards)
		{
			s += c.toString();
		}
		return s;
	}
	
	public ArrayList<Card> getCards(){
		return cards;
	}
	
	public ArrayList<Card> getDrawCards(){
		return drawcards;
	}
}
